home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / emcs1857 / 1857sr~1.zoo / src / dispnew.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-02  |  43.0 KB  |  1,632 lines

  1. /* Newly written part of redisplay code.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990, 1991
  3.         Free Software Foundation, Inc.
  4.  
  5. This file is part of GNU Emacs.
  6.  
  7. GNU Emacs is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 1, or (at your option)
  10. any later version.
  11.  
  12. GNU Emacs is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with GNU Emacs; see the file COPYING.  If not, write to
  19. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /**
  22.  ** (sjk)++ Include time.h for the input-wait loop.
  23.  **/
  24. #if defined(atarist)
  25. #include <time.h>
  26. #endif 
  27.  
  28. /* Modified 1991 for 8-bit character support by Thomas Bellman, modelled
  29.  * after Howard Hayles modifications.  See chartab.c for details. */
  30.  
  31.  
  32. #include <signal.h>
  33.  
  34. #include "config.h"
  35. #include <stdio.h>
  36.  
  37. #ifdef HAVE_TIMEVAL
  38. #ifdef HPUX
  39. #include <time.h>
  40. #else
  41. #include <sys/time.h>
  42. #endif
  43. #endif
  44.  
  45. #ifdef HAVE_TERMIO
  46. #include <termio.h>
  47. #ifdef TCOUTQ
  48. #undef TIOCOUTQ
  49. #define TIOCOUTQ TCOUTQ
  50. #include <fcntl.h>
  51. #endif /* TCOUTQ defined */
  52. #else
  53. #ifndef VMS
  54. #include <sys/ioctl.h>
  55. #endif /* not VMS */
  56. #endif /* not HAVE_TERMIO */
  57.  
  58. /* Allow m- file to inhibit use of FIONREAD.  */
  59. #ifdef BROKEN_FIONREAD
  60. #undef FIONREAD
  61. #endif
  62.  
  63. /* We are unable to use interrupts if FIONREAD is not available,
  64.    so flush SIGIO so we won't try. */
  65. #ifndef FIONREAD
  66. #ifdef SIGIO
  67. #undef SIGIO
  68. #endif
  69. #endif
  70.  
  71. #undef NULL
  72.  
  73. #include "termchar.h"
  74. #include "termopts.h"
  75. #include "cm.h"
  76. #include "lisp.h"
  77. #include "dispextern.h"
  78. #include "buffer.h"
  79. #include "chartab.h"
  80. #include "window.h"
  81. #include "commands.h"
  82.  
  83. #define max(a, b) ((a) > (b) ? (a) : (b))
  84. #define min(a, b) ((a) < (b) ? (a) : (b))
  85.  
  86. #ifndef PENDING_OUTPUT_COUNT
  87. /* Get number of chars of output now in the buffer of a stdio stream.
  88.    This ought to be built in in stdio, but it isn't.
  89.    Some s- files override this because their stdio internals differ.  */
  90. #ifdef __GNU_LIBRARY__
  91. #define    PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bp - (FILE)->__buf)
  92. #else
  93. #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
  94. #endif
  95. #endif /* No PENDING_OUTPUT_COUNT */
  96.  
  97. /* Nonzero means do not assume anything about current
  98.    contents of actual terminal screen */
  99.  
  100. int screen_garbaged;
  101.  
  102. /* Desired terminal cursor position (to show position of point),
  103.    origin zero */
  104.  
  105. int cursor_hpos, cursor_vpos;
  106.  
  107. /* Nonzero means last display completed and cursor is really at
  108.    cursor_hpos, cursor_vpos.  Zero means it was preempted. */
  109.  
  110. int display_completed;
  111.  
  112. /* Lisp variable visible-bell; enables use of screen-flash
  113.    instead of audible bell.  */
  114.  
  115. int visible_bell;
  116.  
  117. /* Invert the color of the whole screen, at a low level.  */
  118.  
  119. int inverse_video;
  120.  
  121. /* Line speed of the terminal.  */
  122.  
  123. int baud_rate;
  124.  
  125. /* nil or a symbol naming the window system
  126.    under which emacs is running
  127.    ('x is the only current possibility).  */
  128.  
  129. Lisp_Object Vwindow_system;
  130.  
  131. /* Version number of window system, or nil if no window system.  */
  132.  
  133. Lisp_Object Vwindow_system_version;
  134.  
  135. /* Nonzero means reading single-character input with prompt
  136.    so put cursor on minibuffer after the prompt.  */
  137.  
  138. int cursor_in_echo_area;
  139.  
  140. /* Description of actual screen contents.  */
  141.  
  142. struct matrix *current_screen;
  143.  
  144. /* Description of desired screen contents.  */
  145.  
  146. struct matrix *new_screen;
  147.  
  148. /* Buffer sometimes used to hold partial screen contents.  */
  149.  
  150. struct matrix *temp_screen;
  151.  
  152. /* Stdio stream being used for copy of all terminal output.  */
  153.  
  154. FILE *termscript;
  155.  
  156. /* Structure for info on cursor positioning */
  157.  
  158. struct cm Wcm;
  159.  
  160. int in_display;        /* 1 if in redisplay: can't handle SIGWINCH now.  */
  161.  
  162. int delayed_size_change;  /* 1 means SIGWINCH happened when not safe.  */
  163. int delayed_screen_height;  /* Remembered new screen height.  */
  164. int delayed_screen_width;   /* Remembered new screen width.  */
  165.  
  166. /* This buffer records the history of display preemption.  */
  167.  
  168. struct preempt
  169. {
  170.   /* Number of keyboard characters read so far at preempt.  */
  171.   int keyboard_char_count;
  172.   /* Vertical position at which preemption occurred.  */
  173.   int vpos;
  174. };
  175.  
  176. #define N_PREEMPTIONS 50
  177.  
  178. /* Circular buffer recording recent display preemptions.  */
  179. struct preempt preemptions[N_PREEMPTIONS];
  180.  
  181. /* Index of next element in preemptions.  */
  182. int preemption_index;
  183.  
  184. /* Set these variables in the debugger to force a display preemption.  */
  185. int debug_preemption_vpos = -1;
  186. int debug_preemption_char_count = -1;
  187.  
  188. extern int num_input_chars;
  189.  
  190. /* Free and reallocate current_screen and new_screen.  */
  191.  
  192. struct matrix *make_screen_structure ();
  193.  
  194. remake_screen_structures ()
  195. {
  196.   if (current_screen)
  197.     free_screen_structure (current_screen);
  198.   if (new_screen)
  199.     free_screen_structure (new_screen);
  200.   if (temp_screen)
  201.     free_screen_structure (temp_screen);
  202.  
  203.   current_screen = make_screen_structure (0);
  204.   new_screen = make_screen_structure (0);
  205.   temp_screen = make_screen_structure (1);
  206.  
  207.   if (message_buf)
  208.     message_buf = (char *) xrealloc (message_buf, screen_width + 1);
  209.   else
  210.     message_buf = (char *) xmalloc (screen_width + 1);
  211. }
  212.  
  213. struct matrix *
  214. make_screen_structure (empty)
  215.      int empty;
  216. {
  217.   int i;
  218.   struct matrix *new = (struct matrix *) xmalloc (sizeof (struct matrix));
  219.  
  220.   new->height = screen_height;
  221.   new->width = screen_width;
  222.   new->highlight = (char *) xmalloc (screen_height);
  223.   new->enable = (char *) xmalloc (screen_height);
  224.   new->contents = (glyf_t **) xmalloc (screen_height * sizeof (glyf_t *));
  225.   new->used = (int *) xmalloc (screen_height * sizeof (int));
  226.   if (empty)
  227.     {
  228.       /* Make the buffer used by decode_mode_spec.  */
  229.       new->total_contents = (glyf_t *) xmalloc ((screen_width + 2)
  230.                         * sizeof (glyf_t));
  231.       bzero (new->contents, screen_height * sizeof (glyf_t *));
  232.     }
  233.   else
  234.     {
  235.       /* Add 2 to leave extra bytes at beginning and end of each line.  */ 
  236.       new->total_contents = (glyf_t *) xmalloc (screen_height
  237.                         * (screen_width + 2)
  238.                         * sizeof (glyf_t));
  239.       bzero (new->total_contents, (screen_height * (screen_width + 2)
  240.                    * sizeof (glyf_t)));
  241.       for (i = 0; i < screen_height; i++)
  242.     new->contents[i] = new->total_contents + i * (screen_width + 2) + 1;
  243.     }
  244.   bzero (new->enable, screen_height);
  245.   return new;
  246. }
  247.  
  248. free_screen_structure (matrix)
  249.      struct matrix *matrix;
  250. {
  251.   if (matrix->total_contents)
  252.     free (matrix->total_contents);
  253.   free (matrix->contents);
  254.   free (matrix->highlight);
  255.   free (matrix->enable);
  256.   free (matrix->used);
  257.   free (matrix);
  258. }
  259.  
  260. /* Return the hash code of contents of line VPOS of screen-matrix M.  */
  261.  
  262. int
  263. line_hash_code (m, vpos)
  264.      struct matrix *m;
  265.      int vpos;
  266. {
  267.   register glyf_t *body;
  268.   register int h = 0;
  269.   /* Give all lighlighted lines the same hash code
  270.      so as to encourage scrolling to leave them in place.  */
  271.   if (m->highlight[vpos])
  272.     return -1;
  273.  
  274.   body = m->contents[vpos];
  275.  
  276.   if (must_write_spaces)
  277.     {
  278.       while (1)
  279.     {
  280.       int c = *body++;
  281.       if (c == 0)
  282.         break;
  283.       h = (((h << 4) + (h >> 24)) & 0x0fffffff) + c - SPACEGLYF;
  284.     }
  285.     }
  286.   else
  287.     {
  288.       while (1)
  289.     {
  290.       int c = *body++;
  291.       if (c == 0)
  292.         break;
  293.       h = (((h << 4) + (h >> 24)) & 0x0fffffff) + c;
  294.     }
  295.     }
  296.   if (h)
  297.     return h;
  298.   return 1;
  299. }
  300.  
  301. /* Return number of characters in line in M at vpos VPOS,
  302.    except don't count leading and trailing spaces
  303.    unless the terminal requires those to be explicitly output.  */
  304.  
  305. int
  306. line_draw_cost (m, vpos)
  307.      struct matrix *m;
  308.      int vpos;
  309. {
  310.   register glyf_t *body, *end;
  311.   register int i;
  312.  
  313.   body = m->contents[vpos];
  314.   end = body + m->used[vpos] - 1;
  315.   if (!must_write_spaces)
  316.     {
  317.       while (*end == SPACEGLYF && end != body)
  318.     end--;
  319.       if (end == body)            /* All blank line */
  320.     return 0;
  321.       while (*body == SPACEGLYF)
  322.     body++;
  323.     }
  324.   i = 0;
  325.   while (body <= end && *body != 0)
  326.     i += glyf_len (*body++);
  327.   return i;
  328. }
  329.  
  330. /* The functions on this page are the interface from xdisp.c to redisplay.
  331.  
  332.  The only other interface into redisplay is through setting
  333.  cursor_hpos and cursor_vpos (in xdisp.c) and setting screen_garbaged. */
  334.  
  335. /* cancel_line eliminates any request to display a line at position `vpos' */
  336.  
  337. cancel_line (vpos)
  338.      int vpos;
  339. {
  340.   new_screen->enable[vpos] = 0;
  341. }
  342.  
  343. clear_screen_records ()
  344. {
  345.   int i;
  346.  
  347.   bzero (current_screen->enable, screen_height);
  348. }
  349.  
  350. /* Get ready to display on line `vpos'
  351.    and set it up for outputting starting at `hpos' within it.
  352.    Return the text string where that line is stored.  */
  353.  
  354. glyf_t *
  355. get_display_line (vpos, hpos)
  356.      int vpos;
  357.      register int hpos;
  358. {
  359.   if (new_screen->enable[vpos] && new_screen->used[vpos] > hpos)
  360.     abort ();
  361.   if (! new_screen->enable[vpos])
  362.     {
  363.       new_screen->used[vpos] = 0;
  364.       new_screen->highlight[vpos] = 0;
  365.       new_screen->enable[vpos] = 1;
  366.     }
  367.  
  368.   if (hpos > new_screen->used[vpos])
  369.     {
  370.       glyf_t *p = new_screen->contents[vpos] + new_screen->used[vpos];
  371.       glyf_t *end = new_screen->contents[vpos] + hpos;
  372.       new_screen->used[vpos] = hpos;
  373.       while (p != end)
  374.     *p++ = SPACEGLYF;
  375.     }
  376.  
  377.   return new_screen->contents[vpos];
  378. }
  379.  
  380. /* Scroll lines from vpos `from' up to but not including vpos `end'
  381.  down by `amount' lines (`amount' may be negative).
  382.  Returns nonzero if done, zero if terminal cannot scroll them. */
  383.  
  384. int
  385. scroll_screen_lines (from, end, amount)
  386.      int from, end, amount;
  387. {
  388.   register int i;
  389.  
  390.   if (!line_ins_del_ok)
  391.     return 0;
  392.  
  393.   if (amount == 0)
  394.     return 1;
  395.   if (amount > 0)
  396.     {
  397.       set_terminal_window (end + amount);
  398.       if (!scroll_region_ok)
  399.     ins_del_lines (end, -amount);
  400.       ins_del_lines (from, amount);
  401.       set_terminal_window (0);
  402.  
  403.       rotate_vector (current_screen->contents + from,
  404.              sizeof (glyf_t *) * (end + amount - from),
  405.              amount * sizeof (glyf_t *));
  406.       safe_bcopy (current_screen->used + from,
  407.           current_screen->used + from + amount,
  408.           (end - from) * sizeof current_screen->used[0]);
  409.       safe_bcopy (current_screen->highlight + from,
  410.           current_screen->highlight + from + amount,
  411.           (end - from) * sizeof current_screen->highlight[0]);
  412.       safe_bcopy (current_screen->enable + from,
  413.           current_screen->enable + from + amount,
  414.           (end - from) * sizeof current_screen->enable[0]);
  415.       /* Mark the lines made empty by scrolling as enabled, empty and
  416.      normal video.  */
  417.       bzero (current_screen->used + from,
  418.          amount * sizeof current_screen->used[0]);
  419.       bzero (current_screen->highlight + from,
  420.          amount * sizeof current_screen->highlight[0]);
  421.       for (i = from; i < from + amount; i++)
  422.     {
  423.       current_screen->contents[i][0] = 0;
  424.       current_screen->enable[i] = 1;
  425.     }
  426.     }
  427.   if (amount < 0)
  428.     {
  429.       set_terminal_window (end);
  430.       ins_del_lines (from + amount, amount);
  431.       if (!scroll_region_ok)
  432.     ins_del_lines (end + amount, -amount);
  433.       set_terminal_window (0);
  434.  
  435.       rotate_vector (current_screen->contents + from + amount,
  436.              sizeof (glyf_t *) * (end - from - amount),
  437.              (end - from) * sizeof (glyf_t *));
  438.       safe_bcopy (current_screen->used + from,
  439.           current_screen->used + from + amount,
  440.           (end - from) * sizeof current_screen->used[0]);
  441.       safe_bcopy (current_screen->highlight + from,
  442.           current_screen->highlight + from + amount,
  443.           (end - from) * sizeof current_screen->highlight[0]);
  444.       safe_bcopy (current_screen->enable + from,
  445.           current_screen->enable + from + amount,
  446.           (end - from) * sizeof current_screen->enable[0]);
  447.       /* Mark the lines made empty by scrolling as enabled, empty and
  448.      normal video.  */
  449.       bzero (current_screen->used + end + amount,
  450.          - amount * sizeof current_screen->used[0]);
  451.       bzero (current_screen->highlight + end + amount,
  452.          - amount * sizeof current_screen->highlight[0]);
  453.       for (i = end + amount; i < end; i++)
  454.     {
  455.       current_screen->contents[i][0] = 0;
  456.       current_screen->enable[i] = 1;
  457.     }
  458.     }
  459.   return 1;
  460. }
  461.  
  462. /* Rotate a vector of SIZE bytes, by DISTANCE bytes.
  463.    DISTANCE may be negative.  */
  464.  
  465. rotate_vector (vector, size, distance)
  466.      char *vector;
  467.      int size;
  468.      int distance;
  469. {
  470.   char *temp = (char *) alloca (size);
  471.  
  472.   if (distance < 0)
  473.     distance += size;
  474.  
  475.   bcopy (vector, temp + distance, size - distance);
  476.   bcopy (vector + size - distance, temp, distance);
  477.   bcopy (temp, vector, size);
  478. }
  479.  
  480. /* Like bcopy except never gets confused by overlap.  */
  481.  
  482. safe_bcopy (from, to, size)
  483.      char *from, *to;
  484.      int size;
  485. {
  486.   register char *endf;
  487.   register char *endt;
  488.  
  489.   if (size == 0)
  490.     return;
  491.   if (from > to)
  492.     {
  493.       /* If destination is lower in memory, we can go from the beginning.  */
  494.       endf = from + size;
  495.       while (from != endf)
  496.     *to++ = *from++;
  497.       return;
  498.     }
  499.  
  500.   /* If destination is higher in memory, we can go backwards from the end.  */
  501.   endf = from + size;
  502.   endt = to + size;
  503.  
  504.   do
  505.     *--endt = *--endf;
  506.   while (endf != from);
  507. }
  508.  
  509. /* After updating a window w that isn't the full screen wide,
  510.  copy all the columns that w does not occupy
  511.  from current_screen to new_screen,
  512.  so that update_screen will not change those columns.  */
  513.  
  514. preserve_other_columns (w)
  515.      struct window *w;
  516. {
  517.   register int vpos;
  518.   int start = XFASTINT (w->left);
  519.   int end = XFASTINT (w->left) + XFASTINT (w->width);
  520.   int bot = XFASTINT (w->top) + XFASTINT (w->height);
  521.  
  522.   for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
  523.     {
  524.       if (current_screen->enable[vpos] && new_screen->enable[vpos])
  525.     {
  526.       if (start > 0)
  527.         {
  528.           int len;
  529.  
  530.           bcopy (current_screen->contents[vpos],
  531.              new_screen->contents[vpos], start * sizeof (glyf_t));
  532.           len = min (start, current_screen->used[vpos]);
  533.           if (new_screen->used[vpos] < len)
  534.         new_screen->used[vpos] = len;
  535.         }
  536.       if (current_screen->used[vpos] > end
  537.           && new_screen->used[vpos] < current_screen->used[vpos])
  538.         {
  539.           while (new_screen->used[vpos] < end)
  540.         new_screen->contents[vpos][new_screen->used[vpos]++] = SPACEGLYF;
  541.           bcopy (current_screen->contents[vpos] + end,
  542.              new_screen->contents[vpos] + end,
  543.              (current_screen->used[vpos] - end) * sizeof (glyf_t));
  544.           new_screen->used[vpos] = current_screen->used[vpos];
  545.         }
  546.     }
  547.     }
  548. }
  549.  
  550. /* On discovering that the redisplay for a window was no good,
  551.  cancel the columns of that window,
  552.  so that when the window is displayed over again
  553.  get_display_line will not complain. */
  554.  
  555. cancel_my_columns (w)
  556.      struct window *w;
  557. {
  558.   register int vpos;
  559.   register int start = XFASTINT (w->left);
  560.   register int bot = XFASTINT (w->top) + XFASTINT (w->height);
  561.  
  562.   for (vpos = XFASTINT (w->top); vpos < bot; vpos++)
  563.     if (new_screen->enable[vpos] && new_screen->used[vpos] >= start)
  564.       new_screen->used[vpos] = start;
  565. }
  566.  
  567. /* These functions try to perform directly and immediately on the screen
  568.    the necessary output for one change in the buffer.
  569.    They may return 0 meaning nothing was done if anything is difficult,
  570.    or 1 meaning the output was performed properly.
  571.    They assume that the screen was up to date before the buffer
  572.    change being displayed.  THey make various other assumptions too;
  573.    see command_loop_1 where these are called.  */
  574.  
  575. int
  576. direct_output_for_insert (g)
  577.      glyf_t g;
  578. {
  579. #ifndef COMPILER_REGISTER_BUG
  580.   register
  581. #endif COMPILER_REGISTER_BUG
  582.     struct window *w = XWINDOW (selected_window);
  583. #ifndef COMPILER_REGISTER_BUG
  584.   register
  585. #endif COMPILER_REGISTER_BUG
  586.     int hpos = cursor_hpos;
  587. #ifndef COMPILER_REGISTER_BUG
  588.   register
  589. #endif COMPILER_REGISTER_BUG
  590.     int vpos = cursor_vpos;
  591.  
  592.   /* Give up if about to continue line */
  593.   if (hpos - XFASTINT (w->left) + 1 + 1 >= XFASTINT (w->width)
  594.  
  595.   /* Avoid losing if cursor is in invisible text off left margin */
  596.       || XINT (w->hscroll) && hpos == XFASTINT (w->left)
  597.     
  598.   /* Give up if cursor outside window (in minibuf, probably) */
  599.       || cursor_vpos < XFASTINT (w->top)
  600.       || cursor_vpos >= XFASTINT (w->top) + XFASTINT (w->height)
  601.  
  602.   /* Give up if cursor not really at cursor_hpos, cursor_vpos */
  603.       || !display_completed
  604.  
  605.   /* Give up if w is minibuffer and a message is being displayed there */
  606.       || EQ (selected_window, minibuf_window) && echo_area_contents)
  607.     return 0;
  608.  
  609.   current_screen->contents[vpos][hpos] = g;
  610.   unchanged_modified = MODIFF;
  611.   beg_unchanged = GPT - BEG;
  612.   XFASTINT (w->last_point) = point;
  613.   XFASTINT (w->last_point_x) = cursor_hpos;
  614.   XFASTINT (w->last_modified) = MODIFF;
  615.  
  616.   reassert_line_highlight (0, cursor_vpos);
  617.   write_glyfs (¤t_screen->contents[vpos][hpos], 1);
  618.   fflush (stdout);
  619.   ++cursor_hpos;
  620.   if (hpos == current_screen->used[vpos])
  621.     {
  622.       current_screen->used[vpos] = hpos + 1;
  623.       current_screen->contents[vpos][hpos + 1] = 0;
  624.     }
  625.   return 1;
  626. }
  627.  
  628. int
  629. direct_output_forward_char (n)
  630.      int n;
  631. {
  632.   register struct window *w = XWINDOW (selected_window);
  633.  
  634.   /* Avoid losing if cursor is in invisible text off left margin */
  635.   if (XINT (w->hscroll) && cursor_hpos == XFASTINT (w->left))
  636.     return 0;
  637.  
  638.   cursor_hpos += n;
  639.   XFASTINT (w->last_point_x) = cursor_hpos;
  640.   XFASTINT (w->last_point) = point;
  641.   move_cursor (cursor_vpos, cursor_hpos);
  642.   fflush (stdout);
  643.   return 1;
  644. }
  645.  
  646. /* Update the actual terminal screen based on the data in new_screen.
  647.    Value is nonzero if redisplay stopped due to pending input.
  648.    FORCE nonzero means do not stop for pending input.  */
  649.  
  650. update_screen (force, inhibit_hairy_id)
  651.      int force;
  652.      int inhibit_hairy_id;
  653. {
  654.   register struct display_line **p;
  655.   register struct display_line *l, *lnew;
  656.   register int i;
  657.   int pause;
  658.   int preempt_count = baud_rate / 2400 + 1;
  659.   extern input_pending;
  660.  
  661.   if (screen_height == 0) abort (); /* Some bug zeros some core */
  662.  
  663.   detect_input_pending ();
  664.   if (!force
  665.       && ((num_input_chars == debug_preemption_char_count
  666.        && debug_preemption_vpos == screen_height - 1)
  667.       || input_pending))
  668.     {
  669.       pause = screen_height;
  670.       goto do_pause;
  671.     }
  672.  
  673.   update_begin ();
  674.  
  675.   if (!line_ins_del_ok)
  676.     inhibit_hairy_id = 1;
  677.  
  678.   /* Don't compute for i/d line if just want cursor motion. */
  679.   for (i = 0; i < screen_height; i++)
  680.     if (new_screen->enable)
  681.       break;
  682.  
  683.   /* Try doing i/d line, if not yet inhibited.  */
  684.   if (!inhibit_hairy_id && i < screen_height)
  685.     force |= scrolling ();
  686.  
  687.   /* Update the individual lines as needed.  Do bottom line first.  */
  688.  
  689.   if (new_screen->enable[screen_height - 1])
  690.     update_line (screen_height - 1);
  691.   for (i = 0; i < screen_height - 1 && (force || !input_pending); i++)
  692.     {
  693.       if (!force && num_input_chars == debug_preemption_char_count
  694.       && debug_preemption_vpos == i)
  695.     break;
  696.       if (new_screen->enable[i])
  697.     {
  698.       /* Flush out every so many lines.
  699.          Also flush out if likely to have more than 1k buffered
  700.          otherwise.   I'm told that telnet connections get really
  701.          screwed by more than 1k output at once.  */
  702.       int outq = PENDING_OUTPUT_COUNT (stdout);
  703.       if (outq > 900
  704.           || (outq > 20 && ((i - 1) % preempt_count == 0)))
  705.         {
  706.           fflush (stdout);
  707.           if (preempt_count == 1)
  708.         {
  709. #ifdef TIOCOUTQ
  710.           if (ioctl (0, TIOCOUTQ, &outq) < 0)
  711.             /* Probably not a tty.  Ignore the error and reset
  712.              * the outq count. */
  713.             outq = PENDING_OUTPUT_COUNT (stdout);
  714. #endif
  715.           outq *= 10;
  716.           sleep (outq / baud_rate);
  717.         }
  718.         }
  719.       if ((i - 1) % preempt_count == 0)
  720.         detect_input_pending ();
  721.       /* Now update this line.  */
  722.       update_line (i);
  723.     }
  724.     }
  725.   pause = (i < screen_height - 1) ? i + 1 : 0;
  726.  
  727.   /* Now just clean up termcap drivers and set cursor, etc.  */
  728.   if (!pause)
  729.     {
  730.       if (cursor_in_echo_area < 0)
  731.     move_cursor (screen_height - 1, 0);
  732.       else if (cursor_in_echo_area > 0
  733.            && !current_screen->enable[screen_height - 1])
  734.     move_cursor (screen_height - 1, 0);
  735.       else if (cursor_in_echo_area)
  736.     move_cursor (screen_height - 1,
  737.              min (screen_width - 1,
  738.               current_screen->used[screen_height - 1]));
  739.       else
  740.     move_cursor (cursor_vpos, max (min (cursor_hpos, screen_width - 1), 0));
  741.     }
  742.  
  743.   update_end ();
  744.  
  745.   if (termscript)
  746.     fflush (termscript);
  747.   fflush (stdout);
  748.  
  749.   /* Here if output is preempted because input is detected.  */
  750.  do_pause:
  751.  
  752.   if (screen_height == 0) abort (); /* Some bug zeros some core */
  753.   display_completed = !pause;
  754.   if (pause)
  755.     {
  756.       preemptions[preemption_index].vpos = pause - 1;
  757.       preemptions[preemption_index].keyboard_char_count = num_input_chars;
  758.       preemption_index++;
  759.       if (preemption_index == N_PREEMPTIONS)
  760.     preemption_index = 0;
  761.     }
  762.  
  763.   bzero (new_screen->enable, screen_height);
  764.   return pause;
  765. }
  766.  
  767. /* Called when about to quit, to check for doing so
  768.    at an improper time.  */
  769.  
  770. void
  771. quit_error_check ()
  772. {
  773.   if (new_screen == 0)
  774.     return;
  775.   if (new_screen->enable[0])
  776.     abort ();
  777.   if (new_screen->enable[screen_height - 1])
  778.     abort ();
  779. }
  780.  
  781. /* Decide what insert/delete line to do, and do it */
  782.  
  783. scrolling ()
  784. {
  785.   int unchanged_at_top, unchanged_at_bottom;
  786.   int window_size;
  787.   int changed_lines;
  788.   int *old_hash = (int *) alloca (screen_height * sizeof (int));
  789.   int *new_hash = (int *) alloca (screen_height * sizeof (int));
  790.   int *draw_cost = (int *) alloca (screen_height * sizeof (int));
  791.   register int i;
  792.   int free_at_end_vpos = screen_height;
  793.   
  794.   /* Compute hash codes of all the lines.
  795.      Also calculate number of changed lines,
  796.      number of unchanged lines at the beginning,
  797.      and number of unchanged lines at the end.  */
  798.  
  799.   changed_lines = 0;
  800.   unchanged_at_top = 0;
  801.   unchanged_at_bottom = screen_height;
  802.   for (i = 0; i < screen_height; i++)
  803.     {
  804.       /* Give up on this scrolling if some old lines are not enabled.  */
  805.       if (!current_screen->enable[i])
  806.     return 0;
  807.       old_hash[i] = line_hash_code (current_screen, i);
  808.       if (!new_screen->enable[i])
  809.     new_hash[i] = old_hash[i];
  810.       else
  811.     new_hash[i] = line_hash_code (new_screen, i);
  812.       if (old_hash[i] != new_hash[i])
  813.     {
  814.       changed_lines++;
  815.       unchanged_at_bottom = screen_height - i - 1;
  816.     }
  817.       else if (i == unchanged_at_top)
  818.     unchanged_at_top++;
  819.       /* If line is not changing, its redraw cost is infinite,
  820.      since we can't redraw it.  */
  821.       if (!new_screen->enable[i])
  822.     draw_cost[i] = INFINITY;
  823.       else
  824.     draw_cost[i] = line_draw_cost (new_screen, i);
  825.     }
  826.  
  827.   /* If changed lines are few, don't allow preemption, don't scroll.  */
  828.   if (changed_lines < baud_rate / 2400 || unchanged_at_bottom == screen_height)
  829.     return 1;
  830.  
  831.   window_size = screen_height - unchanged_at_top - unchanged_at_bottom;
  832.  
  833.   if (scroll_region_ok)
  834.     free_at_end_vpos -= unchanged_at_bottom;
  835.   else if (memory_below_screen)
  836.     free_at_end_vpos = -1;
  837.  
  838.   /* If large window, fast terminal and few lines in common between
  839.      current_screen and new_screen, don't bother with i/d calc.  */
  840.   if (window_size >= 18 && baud_rate > 2400
  841.       && (window_size >=
  842.       10 * scrolling_max_lines_saved (unchanged_at_top,
  843.                       screen_height - unchanged_at_bottom,
  844.                       old_hash, new_hash, draw_cost)))
  845.     return 0;
  846.  
  847.   scrolling_1 (window_size, unchanged_at_top, unchanged_at_bottom,
  848.            draw_cost + unchanged_at_top - 1,
  849.            old_hash + unchanged_at_top - 1,
  850.            new_hash + unchanged_at_top - 1,
  851.            free_at_end_vpos - unchanged_at_top);
  852.  
  853.   return 0;
  854. }
  855.  
  856. update_line (vpos)
  857.      int vpos;
  858. {
  859.   register glyf_t *obody, *nbody, *op1, *op2, *np1;
  860.   int tem;
  861.   int osp, nsp, begmatch, endmatch, olen, nlen;
  862.   glyf_t save;
  863.   glyf_t *temp;
  864.  
  865.   /* Check for highlighting change.  */
  866.   if (new_screen->highlight[vpos]
  867.       != (current_screen->enable[vpos] && current_screen->highlight[vpos]))
  868.     {
  869.       change_line_highlight (new_screen->highlight[vpos], vpos,
  870.                  (current_screen->enable[vpos]
  871.                   ? current_screen->used[vpos] : 0));
  872.       current_screen->enable[vpos] = 0;
  873.     }
  874.   else
  875.     reassert_line_highlight (new_screen->highlight[vpos], vpos);
  876.  
  877.   /* ??? */
  878.   if (! current_screen->enable[vpos])
  879.     {
  880.       olen = 0;
  881.     }
  882.   else
  883.     {
  884.       obody = current_screen->contents[vpos];
  885.       olen = current_screen->used[vpos];
  886.       if (! current_screen->highlight[vpos])
  887.     {
  888.       /* Note obody[-1] is always 0.  */
  889.       if (!must_write_spaces)
  890.         while (obody[olen - 1] == SPACEGLYF)
  891.           olen--;
  892.     }
  893.       else
  894.     {
  895.       /* For an inverse-video line, remember we gave it
  896.          spaces all the way to the screen edge
  897.          so that the reverse video extends all the way across.  */
  898.       while (olen < screen_width - 1)
  899.         obody[olen++] = SPACE;
  900.     }
  901.     }
  902.  
  903.   /* One way or another, this will enable the line being updated.  */
  904.   current_screen->enable[vpos] = 1;
  905.   current_screen->used[vpos] = new_screen->used[vpos];
  906.   current_screen->highlight[vpos] = new_screen->highlight[vpos];
  907.  
  908.   if (!new_screen->enable[vpos])
  909.     {
  910.       nlen = 0;
  911.       goto just_erase;
  912.     }
  913.  
  914.   nbody = new_screen->contents[vpos];
  915.   nlen = new_screen->used[vpos];
  916.  
  917.   /* Pretend trailing spaces are not there at all,
  918.      unless for one reason or another we must write all spaces.  */
  919.   /* We know that the previous character byte contains 0.  */
  920.   if (! new_screen->highlight[vpos])
  921.     {
  922.       if (!must_write_spaces)
  923.     while (nbody[nlen - 1] == SPACEGLYF)
  924.       nlen--;
  925.     }
  926.   else
  927.     {
  928.       /* For an inverse-video line, give it extra trailing spaces
  929.      all the way to the screen edge
  930.      so that the reverse video extends all the way across.  */
  931.       while (nlen < screen_width - 1)
  932.     nbody[nlen++] = SPACEGLYF;
  933.     }
  934.  
  935.   /* If there's no i/d char, quickly do the best we can without it.  */
  936.   if (!char_ins_del_ok)
  937.     {
  938.       int i,j;
  939.  
  940.       for (i = 0; i < nlen; i++)
  941.     {
  942.       if (i >= olen || nbody[i] != obody[i])
  943.         {
  944.           /* We found a non-matching char.  */
  945.           move_cursor (vpos, i);
  946.           for (j = 1; (i + j < nlen &&
  947.                (i + j >= olen || nbody[i+j] != obody[i+j]));
  948.            j++);
  949.           /* Output this run of non-matching chars.  */ 
  950.           write_glyfs (nbody + i, j);
  951.           i += j - 1;
  952.           /* Now find the next non-match.  */
  953.         }
  954.     }
  955.       /* Clear the rest of the line, or the non-clear part of it.  */
  956.       if (olen > nlen)
  957.     {
  958.       move_cursor (vpos, nlen);
  959.       clear_end_of_line (olen);
  960.     }
  961.  
  962.       /* Exchange contents between current_screen and new_screen.  */
  963.       temp = new_screen->contents[vpos];
  964.       new_screen->contents[vpos] = current_screen->contents[vpos];
  965.       current_screen->contents[vpos] = temp;
  966.       return;
  967.     }
  968.  
  969.   if (!olen)
  970.     {
  971.       nsp = (must_write_spaces || new_screen->highlight[vpos])
  972.           ? 0 : count_leading_space_glyfs (nbody);
  973.       if (nlen > nsp)
  974.     {
  975.       move_cursor (vpos, nsp);
  976.       write_glyfs (nbody + nsp, nlen - nsp);
  977.     }
  978.  
  979.       /* Exchange contents between current_screen and new_screen.  */
  980.       temp = new_screen->contents[vpos];
  981.       new_screen->contents[vpos] = current_screen->contents[vpos];
  982.       current_screen->contents[vpos] = temp;
  983.       return;
  984.     }
  985.  
  986.   obody[olen] = 1;
  987.   save = nbody[nlen];
  988.   nbody[nlen] = 0;
  989.  
  990.   /* Compute number of leading blanks in old and new contents.  */
  991.   osp = count_leading_space_glyfs (obody);
  992.   if (!new_screen->highlight[vpos])
  993.     nsp = count_leading_space_glyfs (nbody);
  994.   else
  995.     nsp = 0;
  996.  
  997.   /* Compute number of matching chars starting with first nonblank.  */
  998.   begmatch = count_match (obody + osp, nbody + nsp);
  999.  
  1000.   /* Spaces in new match implicit space past the end of old.  */
  1001.   /* A bug causing this to be a no-op was fixed in 18.29.  */
  1002.   if (!must_write_spaces && osp + begmatch == olen)
  1003.     {
  1004.       np1 = nbody + nsp;
  1005.       while (np1[begmatch] == SPACEGLYF)
  1006.     begmatch++;
  1007.     }
  1008.  
  1009.   /* Avoid doing insert/delete char
  1010.      just cause number of leading spaces differs
  1011.      when the following text does not match. */
  1012.   if (begmatch == 0 && osp != nsp)
  1013.     osp = nsp = min (osp, nsp);
  1014.  
  1015.   /* Find matching characters at end of line */
  1016.   op1 = obody + olen;
  1017.   np1 = nbody + nlen;
  1018.   op2 = op1 + begmatch - min (olen - osp, nlen - nsp);
  1019.   while (op1 > op2 && op1[-1] == np1[-1])
  1020.     {
  1021.       op1--;
  1022.       np1--;
  1023.     }
  1024.   endmatch = obody + olen - op1;
  1025.  
  1026.   /* Put correct value back in nbody[nlen].
  1027.      This is important because direct_output_for_insert
  1028.      can write into the line at a later point.  */
  1029.   nbody[nlen] = save;
  1030.  
  1031.   /* tem gets the distance to insert or delete.
  1032.      endmatch is how many characters we save by doing so.
  1033.      Is it worth it?  */
  1034.  
  1035.   tem = (nlen - nsp) - (olen - osp);
  1036.   if (endmatch && tem && endmatch <= DCICcost (tem))
  1037.     endmatch = 0;
  1038.  
  1039.   /* nsp - osp is the distance to insert or delete.
  1040.      begmatch + endmatch is how much we save by doing so.
  1041.      Is it worth it?  */
  1042.  
  1043.   if (begmatch + endmatch > 0 && nsp != osp
  1044.       && begmatch + endmatch <= DCICcost (nsp - osp))
  1045.     {
  1046.       begmatch = 0;
  1047.       endmatch = 0;
  1048.       osp = nsp = min (osp, nsp);
  1049.     }
  1050.  
  1051.   /* Now go through the line, inserting, writing and deleting as appropriate.  */
  1052.  
  1053.   if (osp > nsp)
  1054.     {
  1055.       move_cursor (vpos, nsp);
  1056.       delete_glyfs (osp - nsp);
  1057.     }
  1058.   else if (nsp > osp)
  1059.     {
  1060.       /* If going to delete chars later in line
  1061.      and insert earlier in the line,
  1062.      must delete first to avoid losing data in the insert */
  1063.       if (endmatch && nlen < olen + nsp - osp)
  1064.     {
  1065.       move_cursor (vpos, nlen - endmatch + osp - nsp);
  1066.       delete_glyfs (olen + nsp - osp - nlen);
  1067.       olen = nlen - (nsp - osp);
  1068.     }
  1069.       move_cursor (vpos, osp);
  1070.       insert_spaces (nsp - osp);
  1071.     }
  1072.   olen += nsp - osp;
  1073.  
  1074.   tem = nsp + begmatch + endmatch;
  1075.   if (nlen != tem || olen != tem)
  1076.     {
  1077.       move_cursor (vpos, nsp + begmatch);
  1078.       if (!endmatch || nlen == olen)
  1079.     {
  1080.       /* If new text being written reaches right margin,
  1081.          there is no need to do clear-to-eol at the end.
  1082.          (and it would not be safe, since cursor is not
  1083.          going to be "at the margin" after the text is done) */
  1084.       if (nlen == screen_width)
  1085.         olen = 0;
  1086.       write_glyfs (nbody + nsp + begmatch, nlen - tem);
  1087. #ifdef obsolete
  1088. /* the following code loses disastrously if tem == nlen.
  1089.    Rather than trying to fix that case, I am trying the simpler
  1090.    solution found above.  */
  1091.       /* If the text reaches to the right margin,
  1092.          it will lose one way or another (depending on AutoWrap)
  1093.          to clear to end of line after outputting all the text.
  1094.          So pause with one character to go and clear the line then.  */
  1095.       if (nlen == screen_width && fast_clear_end_of_line && olen > nlen)
  1096.         {
  1097.           /* endmatch must be zero, and tem must equal nsp + begmatch */
  1098.           write_glyfs (nbody + tem, nlen - tem - 1);
  1099.           clear_end_of_line (olen);
  1100.           olen = 0;        /* Don't let it be cleared again later */
  1101.           write_glyfs (nbody + nlen - 1, 1);
  1102.         }
  1103.       else
  1104.         write_glyfs (nbody + nsp + begmatch, nlen - tem);
  1105. #endif
  1106.     }
  1107.       else if (nlen > olen)
  1108.     {
  1109.       write_glyfs (nbody + nsp + begmatch, olen - tem);
  1110.       insert_glyfs (nbody + nsp + begmatch + olen - tem, nlen - olen);
  1111.       olen = nlen;
  1112.     }
  1113.       else if (olen > nlen)
  1114.     {
  1115.       write_glyfs (nbody + nsp + begmatch, nlen - tem);
  1116.       delete_glyfs (olen - nlen);
  1117.       olen = nlen;
  1118.     }
  1119.     }
  1120.  
  1121.  just_erase:
  1122.   /* If any unerased characters remain after the new line, erase them.  */
  1123.   if (olen > nlen)
  1124.     {
  1125.       move_cursor (vpos, nlen);
  1126.       clear_end_of_line (olen);
  1127.     }
  1128.   
  1129.   /* Exchange contents between current_screen and new_screen.  */
  1130.   temp = new_screen->contents[vpos];
  1131.   new_screen->contents[vpos] = current_screen->contents[vpos];
  1132.   current_screen->contents[vpos] = temp;
  1133. }
  1134.  
  1135. int
  1136. count_leading_space_glyfs (r)
  1137.      register glyf_t *r;
  1138. {
  1139.   register int i = 0;
  1140.  
  1141.   while (*r == SPACEGLYF)
  1142.     {
  1143.       r++;
  1144.       i++;
  1145.     }
  1146.   return i;
  1147. }
  1148.  
  1149. int
  1150. count_match (r1, r2)
  1151.      register glyf_t *r1, *r2;
  1152. {
  1153.   register int i = 0;
  1154.  
  1155.   while (*r1 == *r2)
  1156.     {
  1157.       r1++;
  1158.       r2++;
  1159.       i++;
  1160.     }
  1161.   return i;
  1162. }
  1163.  
  1164. #ifdef NOTDEF
  1165. count_blanks (str)
  1166.      char *str;
  1167. {
  1168.   register char *p = str;
  1169.   while (*str++ == ' ');
  1170.   return str - p - 1;
  1171. }
  1172.  
  1173. count_match (str1, str2)
  1174.      char *str1, *str2;
  1175. {
  1176.   register char *p1 = str1;
  1177.   register char *p2 = str2;
  1178.   while (*p1++ == *p2++);
  1179.   return p1 - str1 - 1;
  1180. }
  1181. #endif /* NOTDEF */
  1182.  
  1183. DEFUN ("open-termscript", Fopen_termscript, Sopen_termscript,
  1184.   1, 1, "FOpen termscript file: ",
  1185.   "Start writing all terminal output to FILE as well as the terminal.\n\
  1186. FILE = nil means just close any termscript file currently open.")
  1187.   (file)
  1188.      Lisp_Object file;
  1189. {
  1190.   if (termscript != 0) fclose (termscript);
  1191.   termscript = 0;
  1192.  
  1193.   if (! NULL (file))
  1194.     {
  1195.       file = Fexpand_file_name (file, Qnil);
  1196.       termscript = fopen ((char *)XSTRING (file)->data, "w");
  1197.       if (termscript == 0)
  1198.     report_file_error ("Opening termscript", Fcons (file, Qnil));
  1199.     }
  1200.   return Qnil;
  1201. }
  1202.  
  1203. DEFUN ("set-screen-height", Fset_screen_height, Sset_screen_height, 1, 2, 0,
  1204.   "Tell redisplay that the screen has LINES lines.\n\
  1205. Optional second arg non-nil means that redisplay should use LINES lines\n\
  1206. but that the idea of the actual height of the screen should not be changed.")
  1207.   (n, pretend)
  1208.      Lisp_Object n, pretend;
  1209. {
  1210.   CHECK_NUMBER (n, 0);
  1211.   change_screen_size (XINT (n), 0, !NULL (pretend));
  1212.   return Qnil;
  1213. }
  1214.  
  1215. DEFUN ("set-screen-width", Fset_screen_width, Sset_screen_width, 1, 2, 0,
  1216.   "Tell redisplay that the screen has COLS columns.\n\
  1217. Optional second arg non-nil means that redisplay should use COLS columns\n\
  1218. but that the idea of the actual width of the screen should not be changed.")
  1219.   (n, pretend)
  1220.      Lisp_Object n, pretend;
  1221. {
  1222.   CHECK_NUMBER (n, 0);
  1223.   change_screen_size (0, XINT (n), !NULL (pretend));
  1224.   return Qnil;
  1225. }
  1226.  
  1227. DEFUN ("screen-height", Fscreen_height, Sscreen_height, 0, 0, 0,
  1228.   "Return number of lines on screen available for display.")
  1229.   ()
  1230. {
  1231.   return make_number (screen_height);
  1232. }
  1233.  
  1234. DEFUN ("screen-width", Fscreen_width, Sscreen_width, 0, 0, 0,
  1235.   "Return number of columns on screen available for display.")
  1236.   ()
  1237. {
  1238.   return make_number (screen_width);
  1239. }
  1240.  
  1241. #ifdef SIGWINCH
  1242. window_change_signal ()
  1243. {
  1244.   int width, height;
  1245.   extern int errno;
  1246.   int old_errno = errno;
  1247.  
  1248.   get_screen_size (&width, &height);
  1249.   /* Record the new size, but don't reallocate the data structures now.
  1250.      Let that be done later outside of the signal handler.  */
  1251.   in_display++;
  1252.   change_screen_size (height, width, 0);
  1253.   in_display--;
  1254.   signal (SIGWINCH, (__Sigfunc)window_change_signal);
  1255.  
  1256.   errno = old_errno;
  1257. }
  1258. #endif /* SIGWINCH */
  1259.  
  1260. /* Do any change in screen size that was requested by a signal.  */
  1261.  
  1262. do_pending_window_change ()
  1263. {
  1264.   /* If change_screen_size should have run before, run it now.  */
  1265.   while (delayed_size_change)
  1266.     {
  1267.       int newwidth = delayed_screen_width;
  1268.       int newheight = delayed_screen_height;
  1269.       delayed_size_change = 0;
  1270.       change_screen_size_1 (newheight, newwidth, 0);
  1271.     }
  1272. }
  1273.  
  1274. /* Change the screen height and/or width.  Values may be given as zero to
  1275.    indicate no change is to take place.
  1276.    PRETEND is normally 0; 1 means change used-size only
  1277.    but don't change the size used for calculations;
  1278.    -1 means don't redisplay.  */
  1279.  
  1280. change_screen_size (newlength, newwidth, pretend)
  1281.      register int newlength, newwidth, pretend;
  1282. {
  1283.   /* If we can't deal with the change now, queue it for later.  */
  1284.   if (in_display)
  1285.     {
  1286.       delayed_screen_width = newwidth;
  1287.       delayed_screen_height = newlength;
  1288.       delayed_size_change = 1;
  1289.       return;
  1290.     }
  1291.   delayed_size_change = 0;
  1292.   change_screen_size_1 (newlength, newwidth, pretend);
  1293. }
  1294.  
  1295. change_screen_size_1 (newlength, newwidth, pretend)
  1296.      register int newlength, newwidth, pretend;
  1297. {
  1298.   if ((newlength == 0 || newlength == screen_height)
  1299.       && (newwidth == 0 || newwidth == screen_width))
  1300.     return;
  1301.   if (newlength && newlength != screen_height)
  1302.     {
  1303.       set_window_height (XWINDOW (minibuf_window)->prev, newlength - 1, 0);
  1304.       XFASTINT (XWINDOW (minibuf_window)->top) = newlength - 1;
  1305.       set_window_height (minibuf_window, 1, 0);
  1306.       screen_height = newlength;
  1307.       if (pretend <= 0)
  1308.     ScreenRows = newlength;
  1309.       set_terminal_window (0);
  1310.     }
  1311.   if (newwidth && newwidth != screen_width)
  1312.     {
  1313.       set_window_width (XWINDOW (minibuf_window)->prev, newwidth, 0);
  1314.       set_window_width (minibuf_window, newwidth, 0);
  1315.       screen_width = newwidth;
  1316.       if (pretend <= 0)
  1317.     ScreenCols = newwidth;
  1318.     }
  1319.   remake_screen_structures ();
  1320.   screen_garbaged = 1;
  1321.   calculate_costs ();
  1322.   if (pretend >= 0)
  1323.     redisplay_preserve_echo_area ();
  1324. }
  1325.  
  1326. DEFUN ("baud-rate", Fbaud_rate, Sbaud_rate, 0, 0, 0,
  1327.   "Return the output baud rate of the terminal.")
  1328.   ()
  1329. {
  1330.   Lisp_Object temp;
  1331.   XSET (temp, Lisp_Int, baud_rate);
  1332.   return temp;
  1333. }
  1334.  
  1335. DEFUN ("send-string-to-terminal", Fsend_string_to_terminal,
  1336.   Ssend_string_to_terminal, 1, 1, 0,
  1337.   "Send STRING to the terminal without alteration.\n\
  1338. Control characters in STRING will have terminal-dependent effects.")
  1339.   (str)
  1340.      Lisp_Object str;
  1341. {
  1342.   CHECK_STRING (str, 0);
  1343.   fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, stdout);
  1344.   fflush (stdout);
  1345.   if (termscript)
  1346.     {
  1347.       fwrite (XSTRING (str)->data, 1, XSTRING (str)->size, termscript);
  1348.       fflush (termscript);
  1349.     }
  1350.   return Qnil;
  1351. }
  1352.  
  1353. DEFUN ("ding", Fding, Sding, 0, 1, 0,
  1354.   "Beep, or flash the screen.\n\
  1355. Terminates any keyboard macro currently executing unless an argument\n\
  1356. is given.")
  1357.   (arg)
  1358.   Lisp_Object arg;
  1359. {
  1360.   if (!NULL (arg))
  1361.     {
  1362.       bell ();
  1363.       fflush (stdout);
  1364.     }
  1365.   else
  1366.     bell ();
  1367.   return Qnil;
  1368. }
  1369.  
  1370. bell ()
  1371. {
  1372.   if (noninteractive)
  1373.     putchar (07);
  1374.   else if (!FROM_KBD)  /* Stop executing a keyboard macro. */
  1375.     error ("Keyboard macro terminated by a command ringing the bell");
  1376.   else
  1377.     ring_bell ();
  1378.   fflush (stdout);
  1379. }
  1380.  
  1381. DEFUN ("sleep-for", Fsleep_for, Ssleep_for, 1, 1, 0,
  1382.   "Pause, without updating display, for ARG seconds.")
  1383.   (n)
  1384.      Lisp_Object n;
  1385. {
  1386.   register int t;
  1387. #ifndef subprocesses
  1388. #ifdef HAVE_TIMEVAL
  1389.   struct timeval timeout, end_time, garbage1;
  1390. #endif /* HAVE_TIMEVAL */
  1391. #endif /* no subprocesses */
  1392.  
  1393.   CHECK_NUMBER (n, 0);
  1394.   t = XINT (n);
  1395.   if (t <= 0)
  1396.     return Qnil;
  1397.  
  1398. #ifdef subprocesses
  1399.   wait_reading_process_input (t, 0, 0);
  1400. #else /* No subprocesses */
  1401.   immediate_quit = 1;
  1402.   QUIT;
  1403.  
  1404. #ifdef VMS
  1405.   sys_sleep (t);
  1406. #else /* not VMS */
  1407. /* The reason this is done this way 
  1408.     (rather than defined (H_S) && defined (H_T))
  1409.    is because the VMS preprocessor doesn't grok `defined' */
  1410. #ifdef HAVE_SELECT
  1411. #ifdef HAVE_TIMEVAL
  1412.   gettimeofday (&end_time, &garbage1);
  1413.   end_time.tv_sec += t;
  1414.  
  1415.   while (1)
  1416.     {
  1417.       gettimeofday (&timeout, &garbage1);
  1418.       timeout.tv_sec = end_time.tv_sec - timeout.tv_sec;
  1419.       timeout.tv_usec = end_time.tv_usec - timeout.tv_usec;
  1420.       if (timeout.tv_usec < 0)
  1421.     timeout.tv_usec += 1000000,
  1422.       timeout.tv_sec--;
  1423.       if (timeout.tv_sec < 0)
  1424.     break;
  1425.       if (!select (1, 0, 0, 0, &timeout))
  1426.     break;
  1427.     }
  1428. #else /* not HAVE_TIMEVAL */
  1429.   /* Is it safe to quit out of `sleep'?  I'm afraid to trust it.  */
  1430.   sleep (t);
  1431. #endif /* HAVE_TIMEVAL */
  1432. #else /* not HAVE_SELECT */
  1433.   sleep (t);
  1434. #endif /* HAVE_SELECT */
  1435. #endif /* not VMS */
  1436.   
  1437.   immediate_quit = 0;
  1438. #endif /* no subprocesses */
  1439.   return Qnil;
  1440. }
  1441.  
  1442. DEFUN ("sit-for", Fsit_for, Ssit_for, 1, 2, 0,
  1443.   "Perform redisplay, then wait for ARG seconds or until input is available.\n\
  1444. Optional second arg non-nil means don't redisplay.\n\
  1445. Redisplay is preempted as always if input arrives, and does not happen\n\
  1446. if input is available before it starts.\n\
  1447. Value is t if waited the full time with no input arriving.")
  1448.   (n, nodisp)
  1449.      Lisp_Object n, nodisp;
  1450. {
  1451. #ifndef subprocesses
  1452. #ifdef HAVE_TIMEVAL
  1453.   struct timeval timeout;
  1454. #else
  1455.   int timeout_sec;
  1456. #endif
  1457.   int waitchannels;
  1458. #endif /* no subprocesses */
  1459.  
  1460.   CHECK_NUMBER (n, 0);
  1461.  
  1462.   if (detect_input_pending ())
  1463.     return Qnil;
  1464.  
  1465. if (EQ (nodisp, Qnil))
  1466.     redisplay_preserve_echo_area ();
  1467. /**
  1468.  ** (sjk)++ check for input, quite after start seconds.
  1469.  **/
  1470. #if defined(atarist)
  1471. if (XINT(n) != 0)
  1472.   { unsigned int start; 
  1473.     start = clock();
  1474.     while (clock() - start < XINT(n) * CLK_TCK)
  1475.       if (console_input_status(fileno(stdin)) != 0) return(Qt);
  1476.     return(Qnil);
  1477.   }
  1478.  else return(Qnil);
  1479. #else 
  1480.  
  1481.   if (XINT (n) > 0)
  1482.     {
  1483. #ifdef subprocesses
  1484. #ifdef SIGIO
  1485.       gobble_input ();
  1486. #endif                /* SIGIO */
  1487.       wait_reading_process_input (XINT (n), 1, 1);
  1488. #else                /* no subprocesses */
  1489.       immediate_quit = 1;
  1490.       QUIT;
  1491.  
  1492.       waitchannels = 1;
  1493. #ifdef VMS
  1494.       input_wait_timeout (XINT (n));
  1495. #else                /* not VMS */
  1496. #ifndef HAVE_TIMEVAL
  1497.       timeout_sec = XINT (n);
  1498.       select (1, &waitchannels, 0, 0, &timeout_sec);
  1499. #else                /* HAVE_TIMEVAL */
  1500.       timeout.tv_sec = XINT (n);  
  1501.       timeout.tv_usec = 0;
  1502.       select (1, &waitchannels, 0, 0, &timeout);
  1503. #endif                /* HAVE_TIMEVAL */
  1504. #endif                /* not VMS */
  1505.  
  1506.       immediate_quit = 0;
  1507. #endif                /* no subprocesses */
  1508.     }
  1509.   return detect_input_pending () ? Qnil : Qt;
  1510. #endif /* ! atarist */ 
  1511. }
  1512.  
  1513. char *terminal_type;
  1514.  
  1515. /* Initialization done when Emacs fork is started, before doing stty. */
  1516. /* Determine terminal type and set terminal_driver */
  1517. /* Then invoke its decoding routine to set up variables
  1518.   in the terminal package */
  1519.  
  1520. init_display ()
  1521. {
  1522. #ifdef HAVE_X_WINDOWS
  1523.   extern Lisp_Object Vxterm;
  1524.   Vxterm = Qnil;
  1525. #endif
  1526.  
  1527.   Vwindow_system = Qnil;
  1528. /** 
  1529.  ** (sjk)++ We do have an alternate key...
  1530.  **/
  1531. #if !defined(atarist)
  1532.   meta_key = 0;
  1533. #else 
  1534.   meta_key = 1;
  1535. #endif
  1536.  
  1537.   inverse_video = 0;
  1538.   cursor_in_echo_area = 0;
  1539.   terminal_type = (char *) 0;
  1540.  
  1541.   if (!inhibit_window_system)
  1542.     {
  1543. #ifdef HAVE_X_WINDOWS
  1544.       extern char *alternate_display;
  1545.       char *disp = (char *) egetenv ("DISPLAY");
  1546.  
  1547.       /* Note KSH likes to provide an empty string as an envvar value.  */
  1548.       if (alternate_display || (disp && *disp))
  1549.     {
  1550.       x_term_init ();
  1551.       Vxterm = Qt;
  1552.       Vwindow_system = intern ("x");
  1553. #ifdef X11
  1554.       Vwindow_system_version = make_number (11);
  1555. #else
  1556.       Vwindow_system_version = make_number (10);
  1557. #endif
  1558.       goto term_init_done;
  1559.     }
  1560. #endif /* HAVE_X_WINDOWS */
  1561.       ;
  1562.     }
  1563.   /* Record we aren't using a window system.  */
  1564.   inhibit_window_system = 1;
  1565.  
  1566.   /* Look at the TERM variable */
  1567.   terminal_type = (char *) getenv ("TERM");
  1568.   if (!terminal_type)
  1569.     {
  1570. #ifdef VMS
  1571.       fprintf (stderr, "Please specify your terminal type.\n\
  1572. For types defined in VMS, use  set term /device=TYPE.\n\
  1573. For types not defined in VMS, use  define emacs_term \"TYPE\".\n\
  1574. \(The quotation marks are necessary since terminal types are lower case.)\n");
  1575. #else
  1576.       fprintf (stderr, "Please set the environment variable TERM; see tset(1).\n");
  1577. #endif
  1578.       exit (1);
  1579.     }
  1580.   term_init (terminal_type);
  1581.  
  1582.  term_init_done:
  1583.   remake_screen_structures ();
  1584.   calculate_costs ();
  1585.  
  1586. #ifdef SIGWINCH
  1587. #ifndef CANNOT_DUMP
  1588.   if (initialized)
  1589. #endif /* CANNOT_DUMP */
  1590.     if (inhibit_window_system)
  1591.       signal (SIGWINCH, (__Sigfunc)window_change_signal);
  1592. #endif /* SIGWINCH */
  1593. }
  1594.  
  1595. syms_of_display ()
  1596. {
  1597.   defsubr (&Sopen_termscript);
  1598.   defsubr (&Sding);
  1599.   defsubr (&Ssit_for);
  1600.   defsubr (&Sscreen_height);
  1601.   defsubr (&Sscreen_width);
  1602.   defsubr (&Sset_screen_height);
  1603.   defsubr (&Sset_screen_width);
  1604.   defsubr (&Ssleep_for);
  1605.   defsubr (&Sbaud_rate);
  1606.   defsubr (&Ssend_string_to_terminal);
  1607.  
  1608.   DEFVAR_BOOL ("inverse-video", &inverse_video,
  1609.     "*Non-nil means use inverse-video.");
  1610.   DEFVAR_BOOL ("visible-bell", &visible_bell,
  1611.     "*Non-nil means try to flash the screen to represent a bell.");
  1612.   DEFVAR_BOOL ("no-redraw-on-reenter", &no_redraw_on_reenter,
  1613.     "*Non-nil means no need to redraw entire screen after suspending.\n\
  1614. It is up to you to set this variable to inform Emacs.");
  1615.   DEFVAR_LISP ("window-system", &Vwindow_system,
  1616.     "A symbol naming the window-system under which Emacs is running,\n\
  1617. \(such as `x'), or nil if emacs is running on an ordinary terminal.");
  1618.   DEFVAR_LISP ("window-system-version", &Vwindow_system_version,
  1619.     "Version number of the window system Emacs is running under.");
  1620.   DEFVAR_BOOL ("cursor-in-echo-area", &cursor_in_echo_area,
  1621.     "Non-nil means put cursor in minibuffer after any message displayed there.");
  1622.  
  1623.   /* Initialize `window-system', unless init_display already decided it.  */
  1624. #ifdef CANNOT_DUMP
  1625.   if (noninteractive)
  1626. #endif
  1627.     {
  1628.       Vwindow_system_version = Qnil;
  1629.       Vwindow_system = Qnil;
  1630.     }
  1631. }
  1632.